<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="../js/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" charset="utf-8">

        /*
         * 第一阶段的javascript coding
         */
        var decimalDigits = 2, tax = 5;
        //加法
        function add(x, y) {
            return x + y;
        }

        function subtract(x, y) {
            return x - y;
        }


        /*
         * 第二阶段的javascript coding
         * 原型使用方式1
         */
        var Calculator = function (DigitsA, DigitsB) {
            this.DigitsA = DigitsA;
            this.DigitsB = DigitsB;
        };

        Calculator.prototype = {
            add: function (x, y) {
                return x + y;
            },
            subtract: function (x, y) {
                return x - y;
            }
        };

        /*
         * 扩展：如何分别设置原型的每个属性
         */
        var BaseCalculator = function () {
            //为每个实例都声明一个小数位数
            this.decimalDigits = 2;
        };

        //使用原型给BaseCalculator 扩展2个对象方法
        BaseCalculator.prototype.addFunc = function (x, y) {
            return x + y;
        };
        BaseCalculator.prototype.subtractFunc = function (x, y) {
            return x - y;
        };


        //执行
        $(function () {
//            alert(add(1, 3));
//
//            alert((new Calculator()).subtract(2, 5));
//
//            alert((new BaseCalculator()).addFunc(5, 2));

            //ClickElementId();

            ClickElementId2();

//            printing();

            console.log(isPrime(2));

        });

        /*
         * 获取点击元素的下标
         * 采用【闭包】来解决
         */
        function ClickElementId() {
            var nodes = document.getElementsByTagName("input");
            for (var i = 0; i < nodes.length; i++) {
                nodes[i].addEventListener('click', (function (i) {
                    return function () {
                        console.log('You clicked element #' + i);
                    }
                })(i));
            }
        }
        /*
         * 另一个解决方案不使用IIFE，而是将函数移到循环的外面
         */
        function ClickElementId2() {
            var nodes = document.getElementsByTagName("input");
            for (var i = 0; i < nodes.length; i++) {
                nodes[i].addEventListener("click", handlerWrapper(i));
            }
        }
        function handlerWrapper(i) {
            return function () {
                console.log('You clicked element #' + i);
            }
        }


        /*
         * 提供给setTimeout()和setInterval()的回调都会依次被事件循环处理。
         * 当调用setTimeout()函数时，即使延迟的时间被设置为0，提供的回调也会被排队。
         */
        function printing() {
            console.log(1);
            setTimeout(function () {
                console.log(2);
            }, 1000);
            setTimeout(function () {
                console.log(3);
            }, 0);
            console.log(4);
        }


        /*
         * 判断质数的isPrime()函数，
         * 当其为质数时返回true，否则返回false。
         */
        function isPrime(number) {
            // If your browser doesn't support the method Number.isInteger of ECMAScript 6,
            // you can implement your own pretty easily
            if (typeof number !== 'number' || !Number.isInteger(number)) {
                // Alternatively you can throw an error.
                return false;
            }
            if (number < 2) {
                return false;
            }
            if (number === 2) {
                return true;
            } else if (number % 2 === 0) {
                return false;
            }
            var squareRoot = Math.sqrt(number);
            for (var i = 3; i <= squareRoot; i += 2) {
                if (number % i === 0) {
                    return false;
                }
            }
            return true;
        }


    </script>
</head>
<body>
<input type="button" value="提交">
<input type="button" value="提交">
<input type="button" value="提交">
<input type="button" value="提交">
<input type="button" value="提交">
<input type="button" value="提交">
</body>
</html>